package bot.controller;

import bot.conf.PathConf;
import bot.pojo.*;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/mi")
public class miHoYoController {
    @Autowired
    PathConf PathConf;
    /**
     * 获取登录二维码
     */
    @GetMapping("/getQrImage")
    public String getQrImage(String qqid) throws  Exception {
        //访问米游社获取二维码指向地址
        Map head = new HashMap<>();
        head.put("x-rpc-app_id","bll8iq97cem8");//必须携带这俩请求头，否则无法访问
        head.put("x-rpc-device_id","bll8iq97cem8");
        String s = bot.controller.HttpHelp.sendPost("passport-api.miyoushe.com/account/ma-cn-passport/web/createQRLogin","",head);
        MiData miData = JSONObject.parseObject(s, MiData.class);
        String dataStr = JSONObject.toJSONString(miData.getData());
        //获取data内数据
        String url = JSONObject.parseObject(dataStr).get("url").toString();
        //这个参数用于校验是否扫码成功
        String ticket = JSONObject.parseObject(dataStr).get("ticket").toString();
        String ImgPath = new QRCodeGenerator().generateQRCodeImage(url, 360, 360 , PathConf);
        System.out.println(ticket);
        String start = TimerExample.Start(qqid, ticket);
        if(start.equals("err")){
            return "有未失效的二维码";
        }
        TimerExample.stati.put(qqid,"true");//标记这个群号下有未过期的二维码
        //执行成功将二维码名字返回给bot
        return ImgPath;
    }
//    @NacosValue(value = "${ImgpathConf.normalimgPath}")
//    public String normalimgPath2;

    @NacosValue(value = "${ImgpathConf.normalimgPath}" , autoRefreshed = true)
    public String normalimgPath2;
    @GetMapping("/test")
    public void test(){
        System.out.println(PathConf.QrLoginImgParh);
        System.out.println(PathConf.normalimgPath);
        System.out.println(normalimgPath2);
    }
}
